namespace Dascoba.Umb.ProfileEditor
{
    using System;
    using System.Web;
    using System.Web.Profile;

    /// <summary>
    /// Current Profile
    /// 
    /// Wraps HttpContext.Current.Profile
    /// 
    /// Exposes Get, Set and Save
    /// 
    /// </summary>
    internal static class CurrentProfile
    {
        /// <summary>
        /// Current Context Profile.
        /// </summary>
        /// <returns></returns>
        private static ProfileBase Profile
        {
            get
            {
                return HttpContext.Current.Profile;
            }
        }
        
        /// <summary>
        /// Gets the value of a profile property for the current member profile.
        /// </summary>
        /// <param name="alias">The umbraco property alias.</param>
        /// <returns></returns>
        internal static object GetProperty(string alias)
        {
            var propertyValue = Profile.GetPropertyValue(alias);
            return propertyValue == DBNull.Value ? null : propertyValue;
        }

        /// <summary>
        /// Sets the value of a profile property for the current member profile.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <param name="value">The value.</param>
        internal static void SetProperty(string alias, object value)
        {
            Profile.SetPropertyValue(alias, value);
        }

        /// <summary>
        /// Updates the current profile.
        /// </summary>
        /// <returns></returns>
        public static bool Save()
        {
            try
            {
                Profile.Save();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}